<?php
class Pxexam_ctl extends Ctl{
    function __construct(){
    }
    function main(array $xxfget) {
        $type=!empty($_POST['type'])?$_POST['type']:false;
		$type=!empty($xxfget['type'])?$xxfget['type']:$type;
		if(!$type){
		    $this::show("pxexamlogin");
		}elseif($type=="login"){
		    $idcard=$_POST['idcard'];
		    $pxxyid=xdb::getInstance()->getvar("select py.id from peixunban as pb,peixunxueyuan as py where pb.id=py.pid and kaiqikaoshi like '开启' and idcard like '$idcard' order by pb.id desc limit 1");
		    if($pxxyid==''){
		        echo "号码错误或该场考试还没有开启！";
		    }else{
		        $pxsjid=xdb::getInstance()->getvar("select id from pxshijuan where pxxyid=$pxxyid and zhengbukao like '正考' and status like '未考'");
		        if($pxsjid==''){
		            if((xdb::getInstance()->getvar("select chengji from peixunxueyuan where id=$pxxyid"))>=80){
		                echo "考试已结束!!";
		                exit();
		            }//正考已过的不用再考了
		            $pxsjid=xdb::getInstance()->getvar("select id from pxshijuan where pxxyid=$pxxyid and zhengbukao like '补考' and status like '未考'");
		        }
		        if($pxsjid==''){
		            echo "考试已结束！";
		            exit();
		        }else{
		            $kaoshitime=xdb::getInstance()->getvar("select kaoshitime from pxshijuan where id=$pxsjid");
		            if($kaoshitime==''){//新进入考试的，接下来应该进入信息确认界面了，防止多人同时登陆一个身份证答题
		                $_SESSION['pxxyid']=$pxxyid;
		                $_SESSION['pxsjid']=$pxsjid;
		                xdb::getInstance()->indb("pxsession", array('idcard'=>$idcard), array('idcard'=>$idcard));
		                xdb::getInstance()->updb("pxshijuan", array('kaoshitime'=>date("Y-m-d H:i:s",time())), array('id'=>$pxsjid));
                        echo 1;//登陆考试成功
		            }else{//未知原因中断后再进入考试的
		                $idcardsession=xdb::getInstance()->getvar("select id from pxsession where idcard like '$idcard'");
		                if($idcardsession!=''){
		                    echo "该考生已登陆，不能重复登陆！";
		                }else{
		                    $now=date("Y-m-d H:i:s",time());
		                    $timecha= strtotime($now)-strtotime($kaoshitime);//时间差，秒
		                    if($timecha>5200){
		                        echo "该考试已超时！";
		                    }else{
		                        $_SESSION['pxxyid']=$pxxyid;
		                        $_SESSION['pxsjid']=$pxsjid;
		                        xdb::getInstance()->indb("pxsession", array('idcard'=>$idcard), array('idcard'=>$idcard));
		                        echo 1;//登陆考试成功
		                    }
		                }
		            }
		        }
		    }
		}elseif($type=="exam"){
		    $pxxy=xdb::getInstance()->getrow("select * from peixunxueyuan where id=".$_SESSION['pxxyid']);
		    $pxsj=xdb::getInstance()->getrow("select * from pxshijuan where id=".$_SESSION['pxsjid']);
		    $pxst=xdb::getInstance()->getarray("select * from pxkaoshiti where pxsjid=".$_SESSION['pxsjid']);
		    $this::show("pxexaming2",compact('pxxy','pxsj','pxst'));
		}elseif($type=="dati"){
		    $array=array('daan'=>$_POST['daan']);
		    $array2=array('id'=>$_POST['id']);		    
		    xdb::getInstance()->updb("pxkaoshiti", $array, $array2);
		}elseif($type=="jiaojuan"){//对应文件Pxexaming_v.php，因为不利于多人同时答题，已弃为备用
		    $pxsjid=$_POST['pxsjid'];
		    $dx=explode("_",$_POST['dx']);
		    for($i=1;$i<count($dx);$i++){
		        xdb::getInstance()->updb("pxkaoshiti",array('daan'=>explode(",", $dx[$i])[1]), array('id'=>explode(",", $dx[$i])[0]));
		    }
		    $chengji=xdb::getInstance()->getarray("select biaozhundaan,daan from pxkaoshiti where pxsjid=$pxsjid");
		    $fenshu=0;
		    foreach ($chengji as $row){
		        if($row['biaozhundaan']==$row['daan']){
		            $fenshu++;
		        }
		    }
		    if($_POST['zhengbukao']=="正考"){
		        xdb::getInstance()->updb("peixunxueyuan", array('chengji'=>$fenshu),array('id'=>$_POST['pxxyid']));
		    }else{
		        xdb::getInstance()->updb("peixunxueyuan", array('bukaochengji'=>$fenshu),array('id'=>$_POST['pxxyid']));
		    }
		    xdb::getInstance()->updb("pxshijuan", array('status'=>'已考','endtime'=>date("Y-m-d H:i:s",time())),array('id'=>$_POST['pxsjid']));
		    xdb::getInstance()->deldb("pxsession", array('idcard'=>$_POST['idcard']));
		    echo $fenshu;
		}elseif($type=="jiaojuan2"){
		    $fenshu=$_POST['fenshu'];
		    if($_POST['zhengbukao']=="正考"){
		        xdb::getInstance()->updb("peixunxueyuan", array('chengji'=>$fenshu),array('id'=>$_POST['pxxyid']));
		    }else{
		        xdb::getInstance()->updb("peixunxueyuan", array('bukaochengji'=>$fenshu),array('id'=>$_POST['pxxyid']));
		    }
		    xdb::getInstance()->updb("pxshijuan", array('zuoda'=>$_POST['daan'],'status'=>'已考','endtime'=>date("Y-m-d H:i:s",time())),array('id'=>$_POST['pxsjid']));
		    xdb::getInstance()->deldb("pxsession", array('idcard'=>$_POST['idcard']));
		}elseif($type=="practicelogin"){
		    $this::show("pxpracticelogin");
		}elseif($type=="practice"){
		    $panduan=xdb::getInstance()->getarray("select * from pxtiku where tixing like '判断' order by rand() limit 50");
		    $danxuan=xdb::getInstance()->getarray("select * from pxtiku where tixing like '单选' order by rand() limit 40");
		    $duoxuan=xdb::getInstance()->getarray("select * from pxtiku where tixing like '多选' order by rand() limit 10");
		    $this::show("pxpractice",compact('panduan','danxuan','duoxuan'));
		}
    }
}